package com.authine.cloudpivot.web.api.secondopen.utils;

import com.authine.cloudpivot.engine.api.model.organization.DepartmentModel;
import com.authine.cloudpivot.engine.api.model.organization.UserModel;
import com.authine.cloudpivot.web.api.dubbo.DubboConfigService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Program: web-api
 * @Description 工具類
 * @Author
 * @Date
 */
@Component
public class CommUtil {
    @Autowired
    private DubboConfigService dubboConfigService;

    public List<String> GetDpeptsByUserId(String userId) {
        List<DepartmentModel> departments = this.dubboConfigService.getOrganizationFacade().getDepartmentsByUserId(userId);
        Set<String> ids = new HashSet<String>();
        ids.add(userId);
        for (DepartmentModel depart : departments) {
            ids.add(depart.getId());
            List<DepartmentModel> depart1 = dubboConfigService.getOrganizationFacade().getParentDepartments(depart.getId(), Boolean.FALSE);
            for (DepartmentModel depart2 : depart1) {
                ids.add(depart2.getId());
            }
        }
        return new ArrayList(ids);
    }

    /**
     * 獲取根目蘆
     * @param departmentId
     * @return
     */
    public String getRootDeptId(String departmentId) {
        if (StringUtils.isBlank(departmentId)) return null;
        List<DepartmentModel> parentList = dubboConfigService.getOrganizationFacade().getParentDepartments(departmentId);
        if (CollectionUtils.isEmpty(parentList)) return null;
        DepartmentModel departmentModel = parentList.get(parentList.size() - 1);
        return departmentModel == null ? null : departmentModel.getId();
    }

    /**
     * 根据用户获取部门ids
     * @param userId
     * @return
     */
    public List<String> getDeptIds(String userId) {
        UserModel userModel = dubboConfigService.getOrganizationFacade().getUser(userId);
        if(userModel==null||userModel.getEmployeeNo()==null) {

//            TODO 后面打开验证
            return new ArrayList<>();
        }
        String deptId=userModel.getDepartmentId();
        List<String> deptIdList=getDeptIdsByDeptId(deptId);
        return deptIdList;
    }

    /**
     * 根据部门id获取ids
     * @param deptId
     * @return
     */
    public List<String> getDeptIdsByDeptId(String deptId) {
        List<String> deptIdList=new ArrayList<>();
        if(!org.apache.commons.lang.StringUtils.isBlank(deptId)){
            List<DepartmentModel> parentDepartments = dubboConfigService.getOrganizationFacade().getParentDepartments(deptId);
            deptIdList = parentDepartments.stream().map(DepartmentModel -> DepartmentModel.getId()).collect(Collectors.toList());
        }
        return  deptIdList;
    }
}
